package com.practice.tree;

import com.practice.util.BinaryTree;
import com.practice.util.BinaryTreeNode;

public class SerializeBST {
	private int index;
	
	public BinaryTree serialize(int[] a) {
		index = 0;
		BinaryTreeNode root = serialize(null, a, Integer.MIN_VALUE, Integer.MAX_VALUE);
		return new BinaryTree(root);
	}
	
	private BinaryTreeNode serialize(BinaryTreeNode parent, int[] a, int low, int high) {
		if (index==a.length || a[index] < low || a[index] > high) return null;
		BinaryTreeNode node = new BinaryTreeNode(parent, a[index]);
		index++;
		
		node.addLeft(serialize(node, a, low, node.value()));
		node.addRight(serialize(node, a, node.value(), high));
		return node;
	}
}
